package fragment;

import holder.CategoryNormalHolder;
import holder.CategoryTitleHolder;

import java.util.List;
import java.util.Random;

import factory.ListViewFactory;

import protocol.CategoryProtocol;

import base.BaseFragment;
import base.BaseHolder;
import base.LoadingPager.LoadResult;
import base.SuperBaseAdapter;
import bean.CategoryInfoBean;
import utils.UIUtils;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 分类的Fragment
 * @author Anson-pc
 *
 */
public class CategoryFragment extends BaseFragment {

	private List<CategoryInfoBean> datas;


	/**
	 * 
	 */
	@Override
	protected LoadResult initData() {
		
		//成功
		//失败
		//错误
		
		//随机返回状态
		
		CategoryProtocol protocol = new CategoryProtocol();
		
		try {
			datas = protocol.loadData(0);
			System.out.println("￥￥￥分类的数据： ==="+datas.toString());
			return checkState(datas);
		} catch (Exception e) {
			e.printStackTrace();
			return LoadResult.ERROR;
		}
	}

	@Override
	protected View initSuccessView() {
		ListView lv = ListViewFactory.createListView();
		lv.setAdapter(new CategoryAdapter(lv, datas));
		return lv;
	}
	
	class CategoryAdapter extends SuperBaseAdapter<CategoryInfoBean>{

		public CategoryAdapter(AbsListView ablistView,
				List<CategoryInfoBean> mDatas) {
			super(ablistView, mDatas);
		}

		@Override
		protected BaseHolder<CategoryInfoBean> getSpecialHolder(int position) {
			CategoryInfoBean categoryInfoBean = datas.get(position);
			if (categoryInfoBean.isTitle) {
				//返回标题的显示
				return new CategoryTitleHolder();
			}else {
				//返回holder的显示
				return new CategoryNormalHolder();
			}
		}

		/**
		 * 加载更多的方法
		 */
		@Override
		protected boolean hasLoadMore() {
			// TODO Auto-generated method stub
			return false;
		}
		
		
		@Override
		public int getViewTypeCount() {
			// TODO Auto-generated method stub
			return super.getViewTypeCount() +1;
		}
		 
		@Override
		protected int getNormalItemViewType(int position) {
			CategoryInfoBean categoryInfoBean = datas.get(position);
			if (categoryInfoBean.isTitle) {
				return 1;
			}else{
				return 2;
			}
		}
	}

}
